/*
 * 
 * Libman
 * 
 * Daniel Kaufmann, Martin Lang, Simon Moosbrugger,
 * Stefan Mayer, Stephan Halbeisen
 * 
 */
package application.server;

import java.rmi.RemoteException;
import java.rmi.server.RemoteServer;
import java.rmi.server.ServerNotActiveException;
import java.rmi.server.UnicastRemoteObject;

import javax.rmi.PortableRemoteObject;
import javax.rmi.CORBA.Tie;

import application.controller.server.SessionController;
import application.exceptions.DatabaseException;
import application.exceptions.UserNotInDbException;
import application.exceptions.WrongCredentialsException;

/**
 * The Class ServerImpl.
 */
public class ServerImpl extends UnicastRemoteObject implements Server {
	
	/**
	 * The Thread CheckSessions is for checking all 2 hours for expired session
	 * If session is expired, it will removed from HashMap
	 */
	private class CheckSessions extends Thread {
		
		/* (non-Javadoc)
		 * @see java.lang.Thread#run()
		 */
		public void run() {
		    while(true){
		    	try {
		    		Thread.sleep(1000*60*60*2);
		    		_sc.checkSessions();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
		    }
	    }
	}
	
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 7928519965220352313L;

	/** The _sc. */
	private static SessionController _sc;
	
	/**
	 * Instantiates a new server impl.
	 * 
	 * @throws RemoteException
	 *             the remote exception
	 */
	public ServerImpl() throws RemoteException {
		super();
		
		if(_sc == null){
			_sc = new SessionController();
		}
	}

	/* (non-Javadoc)
	 * @see application.server.Server#getMediumSearchUseCaseController()
	 */
	@Override
	public MediumSearchRemote getMediumSearchUseCaseController()
			throws RemoteException {
		return new MediumSearchRemoteImpl();
	}

	/* (non-Javadoc)
	 * @see application.server.Server#getLoanExtendController()
	 */
	@Override
	public LoanExtendRemote getLoanExtendController() throws RemoteException {
		return new LoanExtendRemoteImpl();
	}

	/* (non-Javadoc)
	 * @see application.server.Server#getMediumReserveUseCaseController()
	 */
	@Override
	public MediumReserveRemote getMediumReserveUseCaseController()
			throws RemoteException {
		return new MediumReserveRemoteImpl();
	}

	/* (non-Javadoc)
	 * @see application.server.Server#getMediumReturnUaseCaseController()
	 */
	@Override
	public MediumReturnRemote getMediumReturnUaseCaseController()
			throws RemoteException {
		return new MediumReturnRemoteImpl();
	}

	/* (non-Javadoc)
	 * @see application.server.Server#getClientSearchUseCaseController()
	 */
	@Override
	public ClientSearchRemote getClientSearchUseCaseController()
			throws RemoteException {
		// TODO Auto-generated method stub
		return new ClientSearchRemoteImpl();
	}

	/* (non-Javadoc)
	 * @see application.server.Server#getExemplarLoanUseCaseController()
	 */
	@Override
	public ExemplarLoanRemote getExemplarLoanUseCaseController()
			throws RemoteException {
		// TODO Auto-generated method stub
		return new ExemplarLoanRemoteImpl();
	}
	
	/* (non-Javadoc)
	 * @see application.server.Server#login(java.lang.String, java.lang.String)
	 */
	public String login(String username, String password) throws RemoteException, ServerNotActiveException, WrongCredentialsException, DatabaseException, UserNotInDbException{
		return _sc.login(username, password, "");
	}
	
	/* (non-Javadoc)
	 * @see application.server.Server#checkSession(java.lang.String)
	 */
	public void logout(String sessionid) throws RemoteException{
		_sc.logout(sessionid);
	}

	@Override
	public GroupPropertiesChangeRemote getGroupPropertiesChangeUseCaseController()
			throws RemoteException {
		return new GroupPropertiesChangeRemoteImpl();
	}
}
